package com.ysyc.driver_ee.fragment;

import android.content.res.ColorStateList;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.activity.MainActivity;
import com.ysyc.driver_ee.base.BaseFragment;
import com.ysyc.driver_ee.impl.ViewPagerChangeListener;
import com.ysyc.driver_ee.widget.CustomViewPager;
import com.ysyc.driver_ee.widget.NoActionBarView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * <p>描述：(这里用一句话描述这个类的作用)</p>
 * 作者： Chan<br>
 * 日期： 2018/10/29 18:32<br>
 * 版本： v1.0<br>
 */
public class StatisticsFragment extends BaseFragment {
    @BindView(R.id.nabv_staistics_view)
    NoActionBarView nabvStaisticsView;
    @BindView(R.id.tl_order_form_view)
    TabLayout tlOrderFormView;
    @BindView(R.id.main_vp)
    CustomViewPager mainVp;

    private StatisticsPersonalFragment personalFragment;
    private DriverStatisticsFragment driverFragment;

    private final String[] TAB_TITLES = new String[]{"个人", "司机"};
    //Tab 数目
    private final int COUNT = TAB_TITLES.length;
    //Fragment 数组
    private final BaseFragment[] TAB_FRAGMENTS = new BaseFragment[COUNT];
    private View view;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_staistics;
    }


    @Override
    protected void onFragmentCreate(View view) {
        super.onFragmentCreate(view);
        FragmentManager supportFragmentManager = getChildFragmentManager();
        String tag = "android:switcher:" + R.id.main_vp + ":";
        personalFragment = (StatisticsPersonalFragment) supportFragmentManager.findFragmentByTag(tag + 0);
        driverFragment = (DriverStatisticsFragment) supportFragmentManager.findFragmentByTag(tag + 1);

        if (personalFragment == null) {
            personalFragment = personalFragment.newInstance(StatisticsPersonalFragment.class, null);
        }
        if (driverFragment == null) {
            driverFragment = driverFragment.newInstance(DriverStatisticsFragment.class, null);
        }

        TAB_FRAGMENTS[0] = personalFragment;
        TAB_FRAGMENTS[1] = driverFragment;

        //设置viewpager不能滑动
        mainVp.setScanScroll(false);
        mainVp.setOffscreenPageLimit(1);
        //设置adapter
        StatisticsFragment.MyViewPagerAdapter mAdapter = new StatisticsFragment.MyViewPagerAdapter(getChildFragmentManager());
        mainVp.setAdapter(mAdapter);
        //设置滑动监听
        mainVp.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tlOrderFormView));
        tlOrderFormView.addOnTabSelectedListener(new ViewPagerChangeListener(mainVp, TAB_TITLES));
        //设置自定义的tab
        setTabs();
    }



    /**
     * @description: ViewPager 适配器
     */
    private class MyViewPagerAdapter extends FragmentPagerAdapter {

        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public BaseFragment getItem(int position) {//指定Position所对应的页面的Fragment内容
//            BaseFragment fragment = FragmentFactory.createFragment(position);
            return TAB_FRAGMENTS[position];
        }

        @Override
        public int getCount() {//决定ViewPager页数的总和
            return COUNT;
        }
    }

    /**
     * 设置自定义的tabview
     */
    private void setTabs() {
        tlOrderFormView.setTabRippleColor(ColorStateList.valueOf(this.getResources().getColor(R.color.transparent)));

        for (int i = 0; i < COUNT; i++) {
            TabLayout.Tab tab = tlOrderFormView.newTab();
            if (i == 0) {
                view = LayoutInflater.from(StatisticsFragment.this.getContext()).inflate(R.layout.tablayout_statistics_item, tlOrderFormView, false);
            } else {
                view = LayoutInflater.from(StatisticsFragment.this.getContext()).inflate(R.layout.tablayout_statistics_right_item, tlOrderFormView, false);
            }
            tab.setCustomView(view);
            view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            TextView tabTv = view.findViewById(R.id.tv_tab_item_view);
            tabTv.setText(TAB_TITLES[i]);
            tlOrderFormView.addTab(tab);
            if (i == 0) {
                tab.select();
            }
        }
    }
}
